            
class RaceController extends BaseController {
    def beforeInterceptor = [action:this.&auth, except:['search']] 
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        if(!params.max) params.max = 10
        [ raceList: Race.list( params ) ]
    }

    def show = {
        def race = Race.get( params.id )

        if(!race) {
            flash.message = "Race not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ race : race ] }
    }

    def delete = {
        def race = Race.get( params.id )
        if(race) {
            race.delete()
            flash.message = "Race ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "Race not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def race = Race.get( params.id )

        if(!race) {
            flash.message = "Race not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ race : race ]
        }
    }

    def update = {
        def race = Race.get( params.id )
        if(race) {
            race.properties = params
            if(!race.hasErrors() && race.save()) {
                flash.message = "Race ${params.id} : ${params.name} updated"
                redirect(action:show,id:race.id)
            }
            else {
                render(view:'edit',model:[race:race])
            }
        }
        else {
            flash.message = "Race not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def race = new Race()
        race.properties = params
        return ['race':race]
    }

    def save = {
        def race = new Race(params)
        if(!race.hasErrors() && race.save()) {
            flash.message = "Race ${race.id} : ${race.name} created"
            redirect(action:show,id:race.id)
        }
        else {
            render(view:'create',model:[race:race])
        }
    }
    
    def search = {
    	if (request.method =='POST'){
    		RaceQuery query=new RaceQuery()
    		bindData(query,params)
    		
    		def criteria = Race.createCriteria()
    		def results = criteria{
    			and{
    				like('city','%'+params.city+'%')
    				like('state','%'+params.state+'%')
    				if(query.distance){
    					switch(query.distanceOperator){
    						case RaceQuery.DistanceOperator.AT_LEAST:
    						ge('distance',query.distance)
    						break
    						case RaceQuery.DistanceOperator.EXACTLY:
    						eq('distance',query.distance)
    						break
    						case RaceQuery.DistanceOperator.AT_MOST:
    						le('distance',query.distance)
    						break
    						default:
    						log.error "Found unexpected value for distance"+" operator - ${query.distanceOperator}"
    					}
    				}
    				between('startDateTime',query.minDate,query.maxDate+1)
    			}
    		}
    		render(view:'searchresults', model:[raceList:results])
    	}
    }

}